//
//  BrandDetailView.m
//  HomePage
//
//  Created by xiong qi on 13-5-9.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "BrandDetailView.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"
#import "../../ResourceLoader/ResourceLoader/JSONKit.h"

#define AnimationDuration 0.4


@implementation BrandDetailView
@synthesize rootPath;

- (id)initWithFrame:(CGRect)frame Root:(NSString *)path
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
        
//        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//        self.rootPath= [NSString stringWithFormat:@"%@/paddata/company/Brand/%@",[paths objectAtIndex:0],[dic objectForKey:@"DocumentName"]];
        self.rootPath = path;
        
        leftScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(50, 25, 536, 600)];
        [self addSubview:leftScrollView];
        [leftScrollView release];
        leftScrollView.showsVerticalScrollIndicator = NO;
        leftScrollView.showsHorizontalScrollIndicator = NO;
        
        rightScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(625, 25, 350, 600)];
        [self addSubview:rightScrollView];
        [rightScrollView release];
        rightScrollView.showsVerticalScrollIndicator = NO;
        rightScrollView.showsHorizontalScrollIndicator = NO;
        
        [self RefreshView:[self ReadConfig]];
    }
    return self;
}

-(NSDictionary *)ReadConfig
{
    NSArray * contentArray = [[ResourceLoader getInstance]getTemplatesByPath:rootPath];
    NSDictionary * dic = nil;
    if (contentArray.count >0) {
        NSString * filePath = [NSString stringWithFormat:@"%@/%@",rootPath,[contentArray objectAtIndex:0]];
        
        NSData * data = [NSData dataWithContentsOfFile:filePath];
        dic = [data objectFromJSONData];
    }
    return dic;
}

-(void)RefreshView:(NSDictionary *)dic
{
    [self LoadTexts:[dic objectForKey:@"Contents"]];
    [self LoadAllImages:[dic objectForKey:@"Images"]];
    [self LoadAllVideos:[dic objectForKey:@"Videos"]];
}

-(void)LoadTexts:(NSArray *)array
{
    int count = [array count];
    
    int distanceY = 0;
    
    for (int i=0; i<count; i++) {
        
        NSDictionary * dic = [array objectAtIndex:i];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, distanceY, 450, 25)];
        [leftScrollView addSubview:label];
        [label setBackgroundColor:[UIColor clearColor]];
        [label release];
        [label setTextColor:[UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0]];
        label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = [dic objectForKey:@"Title"];
        
        UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0, 536, 1)];
        [leftScrollView addSubview:textView];
        [textView release];
        textView.editable = NO;
        [textView setBackgroundColor:[UIColor clearColor]];
        [textView setFont:[UIFont fontWithName:@"Arial" size:16]];
        [textView setTextColor:[UIColor colorWithRed:0.545 green:0.545 blue:0.545 alpha:1.0]];
        textView.text = [dic objectForKey:@"Content"];
        textView.frame = CGRectMake(0, distanceY+50, 536, textView.contentSize.height);
        
        distanceY += 50+textView.contentSize.height+60;
    }
    leftScrollView.contentSize = CGSizeMake(leftScrollView.frame.size.width, distanceY>leftScrollView.frame.size.height?distanceY:leftScrollView.frame.size.height);
}

-(void)LoadAllImages:(NSArray *)array
{
    int count = [array count];
    int spacetop = 20;
    
    UIImage *img=[UIImage imageNamed:@"AllAuto_Comparison_Image_BG"];
    img=[img stretchableImageWithLeftCapWidth:15 topCapHeight:12];

    TouchImageView * tempimageview;
    UIImageView * imageView;
    NSString * path;
    
    for (int i=0; i<count; i++) {
        tempimageview = [[TouchImageView alloc] initWithFrame:CGRectMake(0, (spacetop+240)*i, 350, 240)];
        [tempimageview SetImage:img];
        tempimageview.Type = 1;
        [rightScrollView addSubview:tempimageview];
        [tempimageview release];
        tempimageview.delegate = self;
        
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(2,2, tempimageview.frame.size.width-4, tempimageview.frame.size.height-4)];
        [tempimageview addSubview:imageView];
        [imageView release];
        
        path = [NSString stringWithFormat:@"%@/%@",rootPath,[array objectAtIndex:i]];
        
        UIImage * image = [[UIImage alloc] initWithContentsOfFile:path];
        [imageView setImage:image];
        [image release];
        tempimageview.dataSource = path;
    }
    
    rightScrollView.contentSize = CGSizeMake(rightScrollView.frame.size.width, (spacetop+240)*count);
}

-(void)LoadAllVideos:(NSArray *)array
{
    int count = [array count];
    int top = rightScrollView.contentSize.height;
    int spacetop = 20;
    
    UIImage *img=[UIImage imageNamed:@"AllAuto_Comparison_Image_BG"];
    img=[img stretchableImageWithLeftCapWidth:15 topCapHeight:12];
    
    TouchImageView * tempimageview;
    MoviePlayView * movieView;
    NSString * path;
    
    for (int i=0; i<count; i++) {
        tempimageview = [[TouchImageView alloc] initWithFrame:CGRectMake(0, top + (spacetop+240)*i, 350, 240)];
        [tempimageview SetImage:img];
        tempimageview.Type = 2;
        [rightScrollView addSubview:tempimageview];
        [tempimageview release];
        tempimageview.delegate = self;
        
        
        movieView = [[MoviePlayView alloc] initWithFrame:CGRectMake(2,2, tempimageview.frame.size.width-4, tempimageview.frame.size.height-4)];
        [tempimageview addSubview:movieView];
        [movieView release];
        movieView.userInteractionEnabled = NO;
        
        path = [NSString stringWithFormat:@"%@/%@",rootPath,[array objectAtIndex:i]];
        [movieView SetPlayPath:path];
        tempimageview.dataSource = path;
    }
    
    rightScrollView.contentSize = CGSizeMake(rightScrollView.frame.size.width,top + (spacetop+240)*count);
}


-(void)TouchImageViewBeTouch:(TouchImageView *)view
{

    if (mediaview == nil) {
        mediaview = [[MediaView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    }
    mediaview.alpha = 0;
    
    if (view.Type == 1) {
        [mediaview Refresh:(NSString *)view.dataSource Type:0];
    }
    else if(view.Type == 2)
    {
        [mediaview Refresh:(NSString *)view.dataSource Type:1];
    }
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview:mediaview];
    
    [self ShowMediaView];

}

-(void)ShowMediaView
{
    [UIView animateWithDuration:AnimationDuration
                     animations:^{
                         
                         mediaview.alpha = 1.0;
                     }
                     completion:^(BOOL finished){
                         
                         
                     }];
}


-(void)dealloc
{
    self.rootPath = nil;
    SafeRelease(mediaview);
    [super dealloc];
}

@end
